package org.example.slave.packet;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.example.slave.util.ByteUtil;

import java.io.ByteArrayOutputStream;

/**
 * @author dingrui
 * @since 2022/2/16
 * @description
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class EOFPacket extends PacketWithHeader {

    private byte fieldCount;
    private int warningCount;
    private int statusFlag;

    @Override
    public void fromBytes(byte[] data) {
        int index = 0;
        // 1. read field count
        fieldCount = data[index];
        index++;
        // 2. read warning count
        this.warningCount = ByteUtil.readUnsignedShortLittleEndian(data, index);
        index += 2;
        // 3. read status flag
        this.statusFlag = ByteUtil.readUnsignedShortLittleEndian(data, index);
        // end read
    }

    @Override
    public byte[] toBytes() {
        ByteArrayOutputStream out = new ByteArrayOutputStream(5);
        out.write(this.fieldCount);
        ByteUtil.writeUnsignedShortLittleEndian(this.warningCount, out);
        ByteUtil.writeUnsignedShortLittleEndian(this.statusFlag, out);
        return out.toByteArray();
    }
}
